/*eslint-disable */
import common from "@common/api/common";
import { message } from '@common/assets/js/resetMessage';
import utils from "@common/utils/utils";
import constants from "../constants";
import apiConstants from "@jxc/api/constants";

export default {
    namespaced: true,
    state: {
        ...constants,
        setting: null,
    },
    mutations: {
        setSetting(state, setting) {
            state.setting = setting;
        },
    },
    actions: {
        changeAuditState(store, { url, list, type, _vm, msg_name, state_field = 'audit_state' }) {
            if (!_vm.checkSelection(list, true)) {
                return;
            }
            let filter = [], msg = '';
            if (type === 'audit') {
                filter = list.filter(item => item[state_field] === 1);
                msg = '已审核';
            }
            if (type === 'reAudit') {
                filter = list.filter(item => item[state_field] === 0);
                msg = '未审核，无法反审';
            }
            if (utils.isNotEmptyList(filter)) {
                let codeList = filter.map(item => item.code);
                message.error(`${ msg_name }：${ codeList.join('、') } ${ msg }`);
                return;
            }
            let sid_list = list.map(item => item.sid);
            return common.requestData(url, {
                sid_list,
                state: type === 'audit' ? 1 : -1,
            }).then(result => {
                if (result.code !== 0) {
                    return Promise.reject(result);
                }
                message.success(`已${ type === 'audit' ? '审核' : '反审' }`);
                return true;
            }).catch(err => {
                // message.error(err.message);
                message.error({
                    dangerouslyUseHTMLString: true,
                    message: err.message,
                });
                return false;
            });
        },
        handleDeleteData(store, { url, row, list, _vm, msg_name, state_field = 'audit_state' }) {
            return new Promise(resolve => {
                let msg = `是否删除该${ msg_name }？`;
                let sids = [];
                if (row) {
                    if (utils.isNotEmpty(state_field) && row[state_field] === 1) {
                        message.error(`该${ msg_name }已审核，请先反审核再删除！`);
                        resolve(false);
                        return;
                    }
                    sids = [ row.sid ];
                } else {
                    if (!_vm.checkSelection(list, true)) {
                        resolve(false);
                        return;
                    }
                    msg = `是否删除选择的${ msg_name }？`;
                    if (utils.isNotEmpty(state_field)) {
                        let filter = list.filter(item => item[state_field] === 2);
                        if (utils.isNotEmptyList(filter)) {
                            let codeList = filter.map(item => item.code);
                            message.error(`${ msg_name }：${ codeList.join('、') } 已完成，不可删除！`);
                            resolve(false);
                            return;
                        }
                        filter = list.filter(item => item[state_field] === 1);
                        if (utils.isNotEmptyList(filter)) {
                            let codeList = filter.map(item => item.code);
                            message.error(`${ msg_name }：${ codeList.join('、') } 已审核，请先反审核再删除！`);
                            resolve(false);
                            return;
                        }
                    }
                    sids = list.map(item => item.sid);
                }
                _vm.$confirm(msg, "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    common.requestData(url, {
                        sids,
                    }).then(result => {
                        if (result.code !== 0) {
                            return Promise.reject(result);
                        }
                        message.success("删除成功！");
                        resolve(true);
                    }).catch(err => {
                        message.error(err.message);
                        resolve(false);
                    });
                }).catch(() => {
                    resolve(false);
                });
            });
        },
        handleExecuteData(store, { url, row, list, _vm, msg_name, isIning }) {
            return new Promise(resolve => {
                let sid_list = [];
                if (row) {
                    if (row.audit_state === 0) {
                        message.error(`该${ msg_name } 未审核，无法${ isIning ? '入库' : '出库' }！`);
                        resolve(false);
                        return;
                    }
                    if (row.execute_state === 1) {
                        message.error(`该${ msg_name } 已${ isIning ? '入库' : '出库' }！`);
                        resolve(false);
                        return;
                    }
                    sid_list = [ row.sid ];
                } else {
                    if (!_vm.checkSelection(list, true)) {
                        resolve(false);
                        return;
                    }
                    let filter = list.filter(item => item.audit_state === 0);
                    if (utils.isNotEmptyList(filter)) {
                        let codeList = filter.map(item => item.code);
                        message.error(`${ msg_name }：${ codeList.join('、') } 未审核，无法${ isIning ? '入库' : '出库' }！`);
                        resolve(false);
                        return;
                    }
                    filter = list.filter(item => item.execute_state === 1);
                    if (utils.isNotEmptyList(filter)) {
                        let codeList = filter.map(item => item.code);
                        message.error(`${ msg_name }：${ codeList.join('、') } 已${ isIning ? '入库' : '出库' }！`);
                        resolve(false);
                        return;
                    }
                    sid_list = list.map(item => item.sid);
                }
                common.requestData(url, {
                    sid_list,
                }).then(result => {
                    if (result.code !== 0) {
                        return Promise.reject(result);
                    }
                    message.success(`${ isIning ? '入库' : '出库' }成功！`);
                    resolve(true);
                }).catch(err => {
                    message.error(err.message);
                    resolve(false);
                });
            });
        },
        inventoryAuditState(store, { url, list, type, _vm, msg_name }) {
            if (!_vm.checkSelection(list, true)) {
                return;
            }
            let filter = [], msg = '';
            filter = list.filter(item => item.state === 2);
            msg = '已完成';
            if (utils.isNotEmptyList(filter)) {
                let codeList = filter.map(item => item.code);
                message.error(`${ msg_name }：${ codeList.join('、') } ${ msg }`);
                return;
            }
            if (type === 'audit') {
                filter = list.filter(item => item.state === 1);
                msg = '已审核';
            }
            if (type === 'reAudit') {
                filter = list.filter(item => item.state === 0);
                msg = '未审核，无法反审';
            }
            if (utils.isNotEmptyList(filter)) {
                let codeList = filter.map(item => item.code);
                message.error(`${ msg_name }：${ codeList.join('、') } ${ msg }`);
                return;
            }
            let sid_list = list.map(item => item.sid);
            return common.requestData(url, {
                sid_list,
                state: type === 'audit' ? 1 : 0,
            }).then(result => {
                if (result.code !== 0) {
                    return Promise.reject(result);
                }
                message.success(`已${ type === 'audit' ? '审核' : '反审' }`);
                return true;
            }).catch(err => {
                message.error(err.message);
                return false;
            });
        },
        handleReviewData(store, { url, row, _vm, msg_name }) {
            return new Promise(resolve => {
                let msg = `是否复核该${ msg_name }？复核后将无法恢复!`;
                let sid = row.sid;
                _vm.$confirm(msg, "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    common.requestData(url, {
                        inventory_sid: sid,
                        state: 2,
                    }).then(result => {
                        if (result.code !== 0) {
                            return Promise.reject(result);
                        }
                        message.success("复核成功！");
                        resolve(true);
                    }).catch(err => {
                        message.error(err.message);
                        resolve(false);
                    });
                }).catch(() => {
                    resolve(false);
                });
            });
        },
        initSetting(store) {
            return common.requestData(apiConstants.API_GetJxcSetting, {
            }).then(result => {
                let resData = result.data || {};
                store.commit('setSetting', resData);
                return resData;
            }).catch(err => {
                message.error(err.message);
                return Promise.reject(err);
            })
        },
    },
}
